home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1997 December / PC Pro December 1997 CD-Rom coverdisc.iso / symantec / dbAnywh / JAVA.BIN / CLASSES.ZIP / sun / tools / zip / ZipFile.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-12-14  |  4.1 KB  |  207 lines

  1. package sun.tools.zip;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.RandomAccessFile;
  7. import java.util.Enumeration;
  8. import java.util.Hashtable;
  9.  
  10. public class ZipFile implements ZipConstants {
  11.    private RandomAccessFile file;
  12.    private String path;
  13.    private Hashtable table;
  14.    long cenpos;
  15.    private long endpos;
  16.    private long pos;
  17.    private static final int INBUFSIZ = 64;
  18.  
  19.    public ZipFile(String var1) throws ZipFormatException, IOException {
  20.       this.file = new RandomAccessFile(var1, "r");
  21.       this.path = var1;
  22.       this.init();
  23.    }
  24.  
  25.    public ZipFile(File var1) throws ZipFormatException, IOException {
  26.       this(var1.getPath());
  27.    }
  28.  
  29.    public ZipEntry getEntry(String var1) {
  30.       return (ZipEntry)this.table.get(var1);
  31.    }
  32.  
  33.    public InputStream getInputStream(ZipEntry var1) throws ZipFormatException, IOException {
  34.       return new ZipFileInputStream(this, var1);
  35.    }
  36.  
  37.    public String getPath() {
  38.       return this.path;
  39.    }
  40.  
  41.    public Enumeration entries() {
  42.       return this.table.elements();
  43.    }
  44.  
  45.    public void close() throws IOException {
  46.       if (this.file != null) {
  47.          this.file.close();
  48.          this.file = null;
  49.       }
  50.  
  51.    }
  52.  
  53.    synchronized int read(long var1, byte[] var3, int var4, int var5) throws IOException {
  54.       if (var1 != this.pos) {
  55.          this.file.seek(var1);
  56.       }
  57.  
  58.       int var6 = this.file.read(var3, var4, var5);
  59.       if (var6 > 0) {
  60.          this.pos = var1 + (long)var6;
  61.       }
  62.  
  63.       return var6;
  64.    }
  65.  
  66.    synchronized int read() throws IOException {
  67.       if (this.pos != this.pos) {
  68.          this.file.seek(this.pos);
  69.       }
  70.  
  71.       int var1 = this.file.read();
  72.       if (var1 > 0) {
  73.          ++this.pos;
  74.       }
  75.  
  76.       return var1;
  77.    }
  78.  
  79.    private void init() throws ZipFormatException, IOException {
  80.       if (!this.findEnd()) {
  81.          throw new ZipFormatException("Could not find END header");
  82.       } else {
  83.          byte[] var1 = new byte[22];
  84.          this.file.readFully(var1);
  85.          if (!checkSig(var1, ZipConstants.ENDSIG)) {
  86.             throw new ZipFormatException("Invalid END header signature");
  87.          } else {
  88.             this.cenpos = getLong(var1, 16);
  89.             int var2 = (int)getLong(var1, 12);
  90.             if (this.cenpos + (long)var2 != this.endpos) {
  91.                throw new ZipFormatException("Invalid END header format");
  92.             } else {
  93.                int var3 = getWord(var1, 10);
  94.                if (var3 * 46 > var2) {
  95.                   throw new ZipFormatException("Invalid END header format");
  96.                } else if (getWord(var1, 8) != var3) {
  97.                   throw new ZipFormatException("Contains more than one drive");
  98.                } else {
  99.                   this.file.seek(this.cenpos);
  100.                   byte[] var4 = new byte[var2];
  101.                   this.file.readFully(var4);
  102.                   this.table = new Hashtable(var3);
  103.  
  104.                   int var6;
  105.                   for(int var5 = 0; var5 < var2; var5 += 46 + var6 + getWord(var4, var5 + 30) + getWord(var4, var5 + 32)) {
  106.                      if (!checkSig(var4, var5, ZipConstants.CENSIG)) {
  107.                         throw new ZipFormatException("Invalid CEN header signature");
  108.                      }
  109.  
  110.                      var6 = getWord(var4, var5 + 28);
  111.                      if (var6 == 0 || var5 + 46 + var6 > var2) {
  112.                         throw new ZipFormatException("Invalid CEN header format");
  113.                      }
  114.  
  115.                      String var7 = new String(var4, 0, var5 + 46, var6);
  116.                      ZipEntry var8 = new ZipEntry(var7);
  117.                      var8.locpos = getLong(var4, var5 + 42);
  118.                      if (var8.locpos + getLong(var4, var5 + 20) > this.cenpos) {
  119.                         throw new ZipFormatException("Invalid CEN header format");
  120.                      }
  121.  
  122.                      var8.length = getLong(var4, var5 + 24);
  123.                      var8.mtime = getLong(var4, var5 + 12);
  124.                      if (this.table.put(var8.getPath(), var8) != null) {
  125.                         throw new ZipFormatException("Invalid CEN header format");
  126.                      }
  127.                   }
  128.  
  129.                   if (this.table.size() != var3) {
  130.                      throw new ZipFormatException("Invalid CEN header format");
  131.                   }
  132.                }
  133.             }
  134.          }
  135.       }
  136.    }
  137.  
  138.    private boolean findEnd() throws IOException {
  139.       long var1 = this.file.length();
  140.       this.file.seek(var1);
  141.       long var3 = Math.max(0L, var1 - 65535L);
  142.       byte[] var5 = new byte[68];
  143.       setBytes(var5, 64, 4, 0);
  144.       this.pos = var1;
  145.  
  146.       while(this.pos > var3) {
  147.          int var6 = Math.min((int)(this.pos - var3), 64);
  148.          this.pos -= (long)var6;
  149.          this.file.seek(this.pos);
  150.          this.file.readFully(var5, 0, var6);
  151.  
  152.          while(true) {
  153.             --var6;
  154.             if (var6 <= 0) {
  155.                break;
  156.             }
  157.  
  158.             if (checkSig(var5, var6, ZipConstants.ENDSIG)) {
  159.                this.endpos = this.pos + (long)var6;
  160.                if (var1 - this.endpos >= 22L) {
  161.                   this.file.seek(this.endpos);
  162.                   byte[] var7 = new byte[22];
  163.                   this.file.readFully(var7);
  164.                   int var8 = getWord(var7, 20);
  165.                   if (this.endpos + 22L + (long)var8 == var1) {
  166.                      this.file.seek(this.endpos);
  167.                      this.pos = this.endpos;
  168.                      return true;
  169.                   }
  170.                }
  171.             }
  172.          }
  173.       }
  174.  
  175.       return false;
  176.    }
  177.  
  178.    static final boolean checkSig(byte[] var0, int var1, byte[] var2) {
  179.       for(int var3 = 0; var3 < 4; ++var3) {
  180.          if (var0[var1 + var3] != var2[var3]) {
  181.             return false;
  182.          }
  183.       }
  184.  
  185.       return true;
  186.    }
  187.  
  188.    static final boolean checkSig(byte[] var0, byte[] var1) {
  189.       return checkSig(var0, 0, var1);
  190.    }
  191.  
  192.    static final void setBytes(byte[] var0, int var1, int var2, int var3) {
  193.       while(var2-- > 0) {
  194.          var0[var1++] = (byte)var3;
  195.       }
  196.  
  197.    }
  198.  
  199.    static final int getWord(byte[] var0, int var1) {
  200.       return var0[var1] & 255 | (var0[var1 + 1] & 255) << 8;
  201.    }
  202.  
  203.    static final long getLong(byte[] var0, int var1) {
  204.       return (long)(getWord(var0, var1) | getWord(var0, var1 + 2) << 16);
  205.    }
  206. }
  207.